<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Store\Controller;

use Store\Controller\StoreController;

class GoodsController extends StoreController {

    public function aa(){
        layout(FALSE);
        $this->display();
    }
    //put your code here

    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'goods'); // 数据集
    }

    //审核通过并已上架
    public function index() {
        $goods = M("goods");
        $where['store_id'] = session('store_id');
        $where['goods_state'] = 1;
        $where['goods_sale'] = 1;
        $count = $goods->where($where)->count();
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show = $Page->show(); // 分页显示输出
        $arr = $goods->where($where)->order('goods_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('pages', $show);
        $this->assign('res', $arr);
        $this->assign('action', 'index');
        $this->display();
    }

    //待上架
    public function shelves() {
        $goods = M("goods");
        $where['store_id'] = session('store_id');
        $where['goods_state'] = 1;
        $where['goods_sale'] = 0;
        $count = $goods->where($where)->count();
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $arr = $goods->where($where)->order('goods_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('pages', $show);
        $this->assign('res', $arr);
        $this->assign('action', 'shelves');
        $this->display();
    }

    //回收站
    public function recycle() {
        $goods = M("goods");
        $where['store_id'] = session('store_id');
        $where['goods_state'] = 3;
        $count = $goods->where($where)->count();
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $arr = $goods->where($where)->order('goods_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('pages', $show);
        $this->assign('res', $arr);
        $this->assign('action', 'recycle');
        $this->display();
    }

    public function add() {

        if (IS_POST) {
            $pics = serialize(I('post.goods_images'));
            $datas['store_id'] = session('store_id');
            $datas['store_name'] = session('store_name');
            $datas['goods_title'] = I('goods_title');
            $datas['goods_buytitle'] = I('goods_buytitle');
            $datas['goods_number'] = I('goods_number');
            $datas['goods_store'] = I('goods_store');
            $datas['goods_content'] = cJscript($_POST['goods_content']);
            $thumb = I('goods_thumb');
            if(empty($thumb)){
                $datas['goods_thumb'] = I('post.goods_images')[0];
            }else{
                $datas['goods_thumb'] = $thumb;
            }
            $datas['goods_images'] = $pics;
            $datas['goods_stockprice'] = I('goods_stockprice');
            $datas['goods_price'] = I('goods_price');
            $datas['goods_markprice'] = I('goods_markprice');
            $datas['goods_stock'] = I('goods_stock');
            $datas['goods_class'] = I('class3');
            $datas['stc_id'] = I('stc_id');
            $datas['transport_id'] = I('transport_id');
            $datas['goods_created'] = NOW_TIME;

            //根据最后的商品ID找到各父级的ID
            $class3 = M('category')->where('cat_id=' . $datas['goods_class'])->getField('parent_id');
            if ($class3 == 0) {
                $datas['goods_class1'] = I('class3');
                $datas['goods_class2'] = "";
                $datas['goods_class3'] = "";
            } else {
                $class2 = M('category')->where('cat_id=' . $class3)->getField('parent_id');
                if ($class2 == 0) {
                    $datas['goods_class1'] = $class3;
                    $datas['goods_class2'] = I('class3');
                    $datas['goods_class3'] = "";
                } else {
                    $datas['goods_class1'] = $class2;
                    $datas['goods_class2'] = $class3;
                    $datas['goods_class3'] = I('class3');
                }
            }
            $datas['goods_classname'] = I('goods_classname');
            $datas['goods_sale'] = 1;
            $datas['goods_state'] = 1;
            $tf = M('goods')->data($datas)->add();
            if ($tf) {

                //增加商品的多属性
                $price = I('price');
                $fprice = I('fprice');
                $stock = I('stock');
                $floor = I('floor');
                foreach ($price as $key => $value) {
                    $datas[$key]['goods_id'] = $tf;
                    $datas[$key]['goods_name'] = I('goods_title');
                    $datas[$key]['model'] = $key;
                    $datas[$key]['price'] = $value;
                    $datas[$key]['mktprice'] = $fprice[$key];
                    $datas[$key]['stock'] = $stock[$key];
                    $datas[$key]['floor'] = $floor[$key];
                    M('goodsAttriInfo')->add($datas[$key]);
                }

                //保存属性表
                $depth0 = I('depth0');
                $depth1 = I('depth1');

                foreach ($depth0 as $key => $value) {
                    $de['goods_id'] = $tf;
                    $de['model'] = I('class0');
                    $de['name'] = $value;
                    $de['depth'] = 0;
                    $de['aid'] = $key;
                    M('goodsAttriClass')->add($de);
                }

                if(!empty($depth1)){
                    foreach ($depth1 as $k => $v) {
                        $dea['goods_id'] = $tf;
                        $dea['model'] = I('class1');
                        $dea['name'] = $v;
                        $dea['depth'] = 1;
                        $dea['aid'] = $k;
                        M('goodsAttriClass')->add($dea);
                    }
                }

                //增加商品的参数
                $para_name = I('para_name');
                $para_value = I('para_value');
                foreach ($para_name as $key => $value) {
                    $data[$key]['goods_id'] = $tf;
                    $data[$key]['goods_name'] = I('goods_title');
                    $data[$key]['paraname'] = $value;
                    $data[$key]['value'] = $para_value[$key];
                    M('goodsParaInfo')->add($data[$key]);
                }            
                $this->success('新增成功', U('index'));
            } else {
                $this->error('新增失败');
            }
        } else {
            
            $map['store_id'] = session('store_id');
            //获取店铺的分类
            $storeClass = M('storeGoodsClass')->where($map)->select();
            //获取店铺的运费模版
            $transport = M('transport')->where($map)->select();
            $this->assign('storeClass', $storeClass);
            $this->assign('transport', $transport);
            
            $category = M('category')->field('cat_id,cat_name,parent_id,keywords,cat_desc')->where('parent_id = 0')->select();
            $this->assign('category', $category);
            $this->assign('action', 'add');
            $this->display();
        }
    }

    //编辑商品
    public function edit() {

        $goods_id = I('goods_id');
        if (IS_POST) {
            
            $pics = serialize(I('post.goods_images'));
            $datas['store_id'] = session('store_id');
            $datas['store_name'] = session('store_name');
            $datas['goods_title'] = I('goods_title');
            $datas['goods_buytitle'] = I('goods_buytitle');
            $datas['goods_number'] = I('goods_number');
            $datas['goods_store'] = I('goods_store');
            $datas['goods_content'] = cJscript($_POST['goods_content']);
            $thumb = I('goods_thumb');
            if(empty($thumb)){
                $datas['goods_thumb'] = I('post.goods_images')[0];
            }else{
                $datas['goods_thumb'] = $thumb;
            }
            $datas['goods_images'] = $pics;
            $datas['goods_stockprice'] = I('goods_stockprice');
            $datas['goods_price'] = I('goods_price');
            $datas['goods_markprice'] = I('goods_markprice');
            $datas['goods_stock'] = I('goods_stock');
            $datas['goods_class'] = I('class3');
            $datas['stc_id'] = I('stc_id');
            $datas['transport_id'] = I('transport_id');
            //根据最后的商品ID找到各父级的ID
            $class3 = M('category')->where('cat_id=' . $datas['goods_class'])->getField('parent_id');
            if ($class3 == 0) {
                $datas['goods_class1'] = I('class3');
                $datas['goods_class2'] = "";
                $datas['goods_class3'] = "";
            } else {
                $class2 = M('category')->where('cat_id=' . $class3)->getField('parent_id');
                if ($class2 == 0) {
                    $datas['goods_class1'] = $class3;
                    $datas['goods_class2'] = I('class3');
                    $datas['goods_class3'] = "";
                } else {
                    $datas['goods_class1'] = $class2;
                    $datas['goods_class2'] = $class3;
                    $datas['goods_class3'] = I('class3');
                }
            }
            $datas['goods_classname'] = I('goods_classname');
            $datas['goods_sale'] = 1;
            $datas['goods_state'] = 1;
            $editWhere['goods_id'] = $goods_id;
            $tf = M('goods')->where($editWhere)->save($datas);
            if ($tf !== FALSE) {
                //修改之前先删除所有的属性值和对应的规格分类
                M('goodsAttriInfo')->where($editWhere)->delete();
                M('goodsAttriClass')->where($editWhere)->delete();
                
                //增加商品的多属性
                $price = I('price');
                $fprice = I('fprice');
                $stock = I('stock');
                $floor = I('floor');
                foreach ($price as $key => $value) {
                    $datas[$key]['goods_id'] = $goods_id;
                    $datas[$key]['goods_name'] = I('goods_title');
                    $datas[$key]['model'] = $key;
                    $datas[$key]['price'] = $value;
                    $datas[$key]['mktprice'] = $fprice[$key];
                    $datas[$key]['stock'] = $stock[$key];
                    $datas[$key]['floor'] = $floor[$key];
                    M('goodsAttriInfo')->add($datas[$key]);
                }

                //保存属性表
                $depth0 = I('depth0');
                $depth1 = I('depth1');
                foreach ($depth0 as $key => $value) {
                    $de['goods_id'] = $goods_id;
                    $de['model'] = I('class0');
                    $de['name'] = $value;
                    $de['depth'] = 0;
                    $de['aid'] = $key;
                    M('goodsAttriClass')->add($de);
                }

                foreach ($depth1 as $key => $value) {
                    $dea['goods_id'] = $goods_id;
                    $dea['model'] = I('class1');
                    $dea['name'] = $value;
                    $dea['depth'] = 1;
                    $dea['aid'] = $key;
                    M('goodsAttriClass')->add($dea);
                }
                
                //修改之前先删除所有的商品的参数
                M('goodsParaInfo')->where($editWhere)->delete();
                //增加商品的参数
                $para_name = I('para_name');
                $para_value = I('para_value');
                foreach ($para_name as $key => $value) {
                    $data[$key]['goods_id'] = $goods_id;
                    $data[$key]['goods_name'] = I('goods_title');
                    $data[$key]['paraname'] = $value;
                    $data[$key]['value'] = $para_value[$key];
                    M('goodsParaInfo')->add($data[$key]);
                }
                
                //修改之前先删除所有的商品的参数
                M('goodsFreight')->where($editWhere)->delete();
                //保存运费
                $freight = I('freight');
                foreach ($freight as $key => $value) {
                    $dataa[$key]['goods_id'] = $goods_id;
                    $dataa[$key]['goods_name'] = I('goods_title');
                    $dataa[$key]['model'] = $key;
                    $dataa[$key]['price'] = $value;
                    M('goodsFreight')->add($dataa[$key]);
                }
                
                $this->success('修改成功', U('index'));
            } else {
                $this->error('修改失败');
            }
        }
        
        $map['store_id'] = session('store_id');
        //获取店铺的分类
        $storeClass = M('storeGoodsClass')->where($map)->select();
        //获取店铺的运费模版
        $transport = M('transport')->where($map)->select();
        $this->assign('storeClass', $storeClass);
        $this->assign('transport', $transport);

        $where['goods_id'] = $goods_id;
        $goods = M('goods')->where($where)->find();
        $goods['images'] = unserialize($goods['goods_images']);

        //获取商品的运费
        $freight = M('goodsFreight')->where($where)->select();
        foreach ($freight as $key => $value) {
            switch ($value['model']) {
                case 'province':
                    $goods['freight']['province'] = $value['price'];
                    break;
                case 'outside':
                    $goods['freight']['outside'] = $value['price'];
                    break;
                case 'special':
                    $goods['freight']['special'] = $value['price'];
                    break;
                default:
                    break;
            }
        }

        //初始化分类
        $category = M('category')->field('cat_id,cat_name,parent_id,keywords,cat_desc')->where('parent_id = 0')->select();
        foreach ($category as $key => $value) {
            $category[$key]['class'] = M('category')->field('cat_id,cat_name,parent_id,keywords,cat_desc')->where('parent_id =' . $value['cat_id'])->select();
            //$category[$key]['tuijian'] = M('category')->where('parent_id ='.$value['cat_id'])->select();
            foreach ($category[$key]['class'] as $k => $v) {
                $category[$key]['class'][$k]['class'] = M('category')->field('cat_id,cat_name,parent_id,keywords,cat_desc')->where('parent_id =' . $v['cat_id'])->select();
            }
        }
        $ca = array('class' => $category);
        $gc_json = json_encode($ca);
        $chooseid_json = json_encode(array($goods['goods_class1'], $goods['goods_class2'], $goods['goods_class3']));
        $this->assign('chooseid_json', $chooseid_json);
        $this->assign('gc_json', $gc_json);

        //初始化商品参数
        $ParaInfo = M('goodsParaInfo')->field('paraname,value')->where($where)->select();
        $this->assign('parainfo', $ParaInfo);

        //初始化商品规格
        $attra = M('goodsAttriInfo')->where($where)->select();
        if(!empty($attra)){
            $att .= '{';
            foreach ($attra as $key => $value) {
                $att .= '"floor['.$value['model'].']":"'.$value['floor'].'",';
                $att .= '"price['.$value['model'].']":"'.$value['price'].'",';
                $att .= '"fprice['.$value['model'].']":"'.$value['mktprice'].'",';
                $att .= '"stock['.$value['model'].']":"'.$value['stock'].'",';
            }
            $att .= '}';
        }else{
            $att="0";
        }
        $this->assign("att", $att);
        
        //初始化商品规格
        $attri = M('goodsAttriClass')->where($where)->group('depth')->select();
        foreach ($attri as $key => $value) {
            $map['goods_id'] = $goods_id;
            $map['depth'] = $value['depth'];
            $attr[$key]['guige'] = $value['model'];
            $attr[$key]['class'] = M('goodsAttriClass')->where($map)->select();
        }
        

        $this->assign("attr", $attr);

        //初始化商品规格表格
        $mapa['goods_id'] = $goods_id;
        $mapa['depth'] = 0;
        $depth0 = M('goodsAttriClass')->where($mapa)->select();
        foreach ($depth0 as $key => $value) {
            $mapb['goods_id'] = $goods_id;
            $mapb['model'] = array('like', '%' . $value['aid'] . '%');
            $depth0[$key]['class'] = M('goodsAttriInfo')->where($mapb)->select();
            foreach ($depth0[$key]['class'] as $k => $v) {
                $model = explode('_', $v['model']);
                $mapp['goods_id'] = $goods_id;
                $mapp['aid'] = $model[1];
                $depth0[$key]['class'][$k]['name'] = M('goodsAttriClass')->where($mapp)->getField('name');
                $depth0[$key]['class'][$k]['aid'] = M('goodsAttriClass')->where($mapp)->getField('aid');
            }
        }
        $this->assign('depth0', $depth0);

        $this->assign('goods', $goods);
        $this->assign('action', 'add');
        $this->display();
    }

    
    
    
    //删除并下架
    public function del() {
        $Id = I('post.data', 0, 'intval');
        
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        $map['goods_state'] = 3;
        $map['goods_sale'] = 0;
        if (M('goods')->where('goods_id=' . $Id)->save($map)){
            $this->success('商品已下架并删除！', U('index'));
        } else {
            $this->error('操作失败！');
        }
    }

    //下架
    public function downGoods() {
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        if (M('goods')->where('goods_id=' . $Id)->save(array('goods_sale'=>0))){
            $this->success('商品已下架！', U('index'));
        } else {
            $this->error('操作失败！');
        }
    }
    
    //上架
    public function upGoods() {
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        if (M('goods')->where('goods_id=' . $Id)->save(array('goods_sale'=>1))){
            $this->success('商品已下架！', U('index'));
        } else {
            $this->error('操作失败！');
        }
    }
    
    //撤销删除
    public function cannelDel() {
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        if (M('goods')->where('goods_id=' . $Id)->save(array('goods_state'=>1,'goods_sale'=>0))){
            $this->success('商品已下架！', U('index'));
        } else {
            $this->error('操作失败！');
        }
    }
    
    
    public function attri() {
        header("Access-Control-Allow-Origin: *");
        $data["cat_id"] = I('id');
        $attri = M('Category')->field('attri')->where($data)->find();
        $where['pid'] = $attri['attri'];
        $where['state'] = 1;
        $arr = M('goodsAttri')->field('id,title,content')->where($where)->select();
        return $this->ajaxReturn($arr);
    }

    public function para() {
        header("Access-Control-Allow-Origin: *");
        $data["cat_id"] = I('id');
        $para = M('Category')->field('para')->where($data)->find();
        $where['pid'] = $para['para'];
        $where['state'] = 1;
        $arr = M('goodsPara')->field('id,title')->where($where)->select();
        return $this->ajaxReturn($arr);
    }

}
